
jsal.module({
    name : 'heist.tests.generator.javascript',
    depends : ['heist'],
    onload : function () {
        if (! heist.tests) heist.tests = {};
		if (! heist.tests.generator) heist.tests.generator = {};
        heist.tests.generator.javascript = new testjs.Test(
            'javascript generation', function () {
		        this.plan(16);

		        this.ok(heist.Parser, 'load the heist parser');

		        var parser = new heist.Parser({ embeddedInCode : false, defaultOutputOn : true });

				var test = this;
				function testOutput (template, expected, message) {
					test.is(parser.comp(template).run(), expected, message);
				}
			
	
				testOutput(
					'Hello World!\n',
					'Hello World!\n',
					'simple content'
				);

				testOutput(
					'1.\n'
				    + ': this.append("2.\\n");\n'
                    + '3.\n',
					'1.\n2.\n3.\n',
					'code line'
				);

				testOutput(
					'1.\n'
					+ ': this.append(\n'
					+ ':   "2.\\n")\n'
					+ '3.\n',
					'1.\n2.\n3.\n',
					'multiple code lines run together'
				);
				
				testOutput(
					'',
					'',
					'empty template produces empty content'
				);

				testOutput(
					': this.append("a");',
					'a',
					'just code line'
				);

				testOutput(
					'<( 1 + 1 )>',
					'2',
					'expression tag'
				);

				testOutput(
					'<( 1 + 1 )><( 2 + 2 )>',
					'24',
					'adjacent expression tags'
				);

				testOutput(
					'<[ this.append("a") ]>',
					'a',
					'code tag'
				);

				testOutput(
					'<[ if (true ]><[ ) this.append("a") ]>',
					'a',
					'adjacent code tags run together'
				);

				testOutput(
					'a|-b-|c',
					'ac',
					'bookends'
				);
				
				test.is(
					parser.comp('<~myMethod>method content</~>').run('myMethod'),
					'method content',
					'call a method'
				);

				testOutput(
					'a<~t(p1, p2)><( p2 )><( p1 )></~>b<[ this.comp("t", "d", "c") ]>e',
					'abcde',
					'call a method from component top-level'
				);

				test.is(
					parser.comp('<~m1(a)><[ this.comp("m2", a * 2) ]></~><~m2(a)><( a )></~>').run('m1', 2),
					'4',
					'call a method from another method'
				);

				var parentComp, childComp;

				parentComp = parser.comp('<~over>in over</~><~called>called: <[ this.comp("over2") ]></~><~over2>in over2</~>');

				childComp = parser.comp('<~m1>m1: <[ this.comp("over") ]></~><~over>oo: <[ this.comp("called") ]></~><~over2>in child oo2</~>');

				childComp.setParentComp(parentComp);

				test.is(childComp.run('m1'), 'm1: oo: called: in child oo2', 'one level component inheritance');

				parentComp = parser.comp('parent content');
				childComp = parser.comp(' child content');

				childComp.setParentComp(parentComp);

				test.is(childComp.run(), 'parent content child content', 'child content follows parent content');
			}
		);
	}
});

